package com.itcat.controller;

import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.param.PageParam;
import com.itcat.mysql.pojo.PmsProductAttributeCategory;
import com.itcat.mysql.pojo.PmsProductCategory;
import com.itcat.mysql.pojo.PmsProductCategoryDto;
import com.itcat.mysql.vo.PmsProductAttributeCategoryVo;
import com.itcat.mysql.vo.PmsProductAttributeVo;
import com.itcat.mysql.vo.PmsProductCategoryVo;
import com.itcat.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 23:10
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;


    /**
     * 分页查询商品属性分类
     * @param pageParam
     * @return
     */
    @GetMapping("/category/list")
    public CommonResult categoryList(PageParam pageParam){
        CommonPage<PmsProductAttributeCategory> page = pmsProductAttributeCategoryService.categoryList(pageParam);
        return CommonResult.success(page);
    }


    /**
     * 添加商品属性分类
     * @param
     * @return
     */
    @PostMapping("/category/create")
    public CommonResult categoryCreate(PmsProductAttributeCategory pmsProductAttributeCategory){
        pmsProductAttributeCategoryService.categoryCreate(pmsProductAttributeCategory);
        return CommonResult.success();
    }

    /**
     * 删除指定商品属性分类
     * @param id
     * @return
     */
    @GetMapping("/category/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        pmsProductAttributeCategoryService.delete(id);
        return CommonResult.success();
    }


    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/category/list/withAttr")
    public CommonResult categoryWithAttr(){
        List<PmsProductAttributeCategoryVo> vos = pmsProductAttributeCategoryService.categoryWithAttr();
        return CommonResult.success(vos);
    }


    /**
     * 更新商品属性分类
     * @param id
     * @param pmsProductAttributeCategory
     * @return
     */
    @PostMapping("/category/update/{id}")
    public CommonResult update(@PathVariable("id") Integer id, PmsProductAttributeCategory pmsProductAttributeCategory){
        pmsProductAttributeCategory.setId(id.longValue());
        pmsProductAttributeCategoryService.update(pmsProductAttributeCategory);
        return CommonResult.success();
    }


    /**
     * 根据id获取商品属性分类
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public CommonResult categoryById(@PathVariable("id") Integer id){
        PmsProductAttributeCategory p = pmsProductAttributeCategoryService.getById(id);
        return CommonResult.success(p);
    }

}
